<?php 
// 发送最终确认
/**
参数列表：
    		$param['mid'] = $mid;
    		$param['title'] = $title;
    		$param['content'] = $content;
    		$param['file'] = $file;
 */

//**************************************通用环境定义开始************************//
set_time_limit(0);
ini_set("memory_limit", -1);
define ('APP', dirname(dirname(__FILE__)));

require_once APP . '/frame/Route.php';
require_once FRAME_ROOT_PATH . '/db/Mysql.php';
require_once APP . '/application/configs/application.php';
require_once APP . '/application/models/mail/class.phpmailer.php';

global $cfg;
Mysql::setInstance("default", $cfg['db']['host'], $cfg['db']['user'], $cfg['db']['password'], $cfg['db']['name'], $cfg['db']['charset']);
if ($cfg ['debug'])
    Mysql::getInstance()->setProfiler(true);

$db = Mysql::getInstance();
$str_args = $argv[1];
$arr_args = unserialize(base64_decode($str_args));

//clog(print_R($arr_args, true));
//**************************************定义结束************************//

// 获取发送用户列表
$arr_send_user = array();

$arr_send_user = $db->fetchAll("select t2.*,t1.* from meetuser t1
		left join user t2 on t2.uid=t1.uid
		where t1.mid=? ", array($arr_args['mid']));


$minfo = $db->fetchRow("select * from meet where mid=?", array($arr_args['mid']));
 
// 用户列表筛选
$arr_list =array();
 
if ($minfo['newsletterkey'] != '')
{
	foreach($arr_send_user as $index => $v){
		$u_reg_item = $v['regitems'];
		$u_reg_item = unserialize($u_reg_item);
		$u_val = $u_reg_item[$minfo['newsletterkey']];
		if (strtolower(trim($u_val)) == strtolower(trim($minfo['newslettervalue']))){
			$arr_list[] = $v;
		}
	}
}


// 获取会议信息
$arr_meet = $db->fetchRow("select * from meet where mid=?", array($arr_args['mid']));


// 注册字段处理
$regitems = $arr_meet['regitems'];
$regitems = unserialize($regitems);
 
if (!is_array($regitems))
	$regitems = array();
 
$arr_reg_items = array();
foreach ($regitems as $v){
	if ($v['type'] <= 6){
		$arr_reg_items[] = $v;
	}
}

// 处理抄送地址
$arr_cc_mail = array();
$arr = explode(";", $arr_args['ccmail']);
if (is_array($arr)){
	foreach($arr as $v){
		$arr_cc_mail[] = trim($v);
	}
}


$mail = new PHPMailer();

//clog(print_R($arr_meet, true));
$arr_mail_log = array();
$arr_mail_log['type'] = 'allsend';

$arr_mail_log['sendargs'] = array();
$arr_mail_log['sendargs']['smtpserver'] = $arr_meet['smtpserver'];
$arr_mail_log['sendargs']['email'] = $arr_meet['email'];
$arr_mail_log['sendargs']['emailpwd'] = $arr_meet['emailpwd'];
$arr_mail_log['sendargs']['emailuser'] = $arr_meet['emailuser'];

$arr_mail_log['sendargs'] = serialize($arr_mail_log['sendargs']);

$arr_mail_log['title'] = $arr_args['title'];
$arr_mail_log['mid'] = $arr_args['mid'];

$mail->SMTPDebug = true;
$mail->SMTPKeepAlive = true;
$mail->IsSMTP();					// 启用SMTP
$mail->Host = $arr_meet['smtpserver'];			//SMTP服务器
$mail->SMTPAuth = true;					//开启SMTP认证
$mail->Username = $arr_meet['email'];			// SMTP用户名
$mail->Password = $arr_meet['emailpwd'];				// SMTP密码
$mail->From = $arr_meet['email'];;			//发件人地址
$mail->FromName = $arr_meet['emailuser'];;				//发件人

$mail->WordWrap = 80;					//设置每行字符长度

$mail->CharSet = "utf-8";				// 这里指定字符集！
$mail->Encoding = "base64";
$mail->IsHTML(true);					// 是否HTML格式邮件

$mail->Subject = $arr_args['title'];			//邮件主题
foreach($arr_cc_mail as $address)
	$mail->AddCC($address);

// 发送
reset($arr_list);
if (count($arr_list) == 0)
	return;

while(list($index, $v) = each($arr_list)){
	$irow = $db->fetchRow("select * from invitecode where mid=? and email='?'", array($arr_args['mid'],$v['email']));
	
	if (!empty($irow)){
		$v['name'] = $irow['name'];
		$v['ncall']  = $irow['ncall'];
		$v['enname'] = $irow['enname'];
		$v['encall']  = $irow['encall'];
	}
	/**
	// 处理称呼
	if ($arr_args['nametype'] == 'zh_cn'){
		$content = '<span style="font-family:'.$arr_args['fontname'].';font-size:'.$arr_args['fontsize'].'pt;">尊敬的' . $v['name']  . "：</span>\n<br />";
	}else{
		$content = '<span style="font-family:'.$arr_args['fontname'].';font-size:'.$arr_args['fontsize'].'pt;">Dear '  .' '. $v['enname'] . "：</span>\n<br />";
	}
	**/
	//$content = '<strong>' . $v['name'] . $v['ncall'] . "：</strong>\n<br />";
	$content = "";
	$content .= $arr_args['content'] . "<br /><br />";
	
	if ($arr_args['nametype'] == 'zh_cn'){
		$content .= '<br /><p>&nbsp;</p>--------------------<p>'.$arr_meet['name'].'会议注册系统：<a target="_blank" href="'.$arr_args['url'].'?lan=cn">'.$arr_args['url'].'</a></p>';
	}else{
		$content .= '<br /><p>&nbsp;</p>--------------------<p>'.$arr_meet['enname'].' Conference Service Platform: <a target="_blank" href="'.$arr_args['url'].'?lan=en">'.$arr_args['url'].'</a></p>';
	}
	
	$mail->Body = $content;
	$mail->ClearAddresses();
	$mail->ClearAttachments();
	
	if (file_exists(APP . $arr_args['file']))
		$mail->AddAttachment(APP . $arr_args['file']);
	
	$mail->AddAddress($v['email']);
		
	
	// 发送成功
	$ret = "ok";
	try{
		if(!$mail->Send()){
			$ret = $mail->ErrorInfo;
		}
	}catch(Exception $e){
		$ret = $e->getMessage();
	}
	
	// 记录发送日志
	$arr_mail_log['sendtime'] = date("Y-m-d H:i:s", time());
	$arr_mail_log['content'] = $content;
	$arr_mail_log['message'] = $ret;
	$arr_mail_log['toemail'] = $v['email'];
	if (!empty($str_args['ccmail'])){
		$arr_mail_log['toemail'] .= ' 抄送：' . $arr_args['ccmail'];
	}
	
	$db->insert("mailog", $arr_mail_log);

	$mail->SmtpClose();
}


function clog($text)
{
	file_put_contents(APP . "/cache/a.log", $text, FILE_APPEND);
}
?>